from django.db import models

# Create your models here.

class UserInfo(models.Model):
    """用户表"""
    code = models.CharField(max_length=32,unique=True)    # 工号
    username = models.CharField(max_length=32)    # 用户名
    password = models.CharField(max_length=255)    # 密码
    is_admin = models.BooleanField(default=False)    # 是否是管理员
    is_active = models.BooleanField(default=True)    # 是否是启用状态


    USERNAME_FIELD = 'username'    # 指定哪个字段作为登录的用户名
    PASSWORD_FIELD = 'password'    # 指定哪个密码作为密码字段
    REQUIRED_FIELDS = ['code']    # 使用命令行创建用户时，除了用户名和密码外所需要的字段

    class Meta:
        db_table = "user_info"

    def __str__(self):
        return self.username

    # def keys(self):
    #     return ('username','password','is_active')
    #
    # def __getitem__(self, item):
    #     return getattr(self,item)


class StockInfo(models.Model):
    """库存信息表"""

    code = models.CharField(max_length=32, unique=True)  # 零件编号
    name = models.CharField(max_length=32,)    # 零件名
    stock = models.IntegerField()    # 库存
    threshold = models.IntegerField()    # 警戒值
    unit = models.CharField(max_length=8)    # 计量单位

    class Meta:
        db_table = "stock_info"


class OperationRecord(models.Model):
    """操作记录表"""

    count = models.IntegerField()    # 数量
    operation_type_choices = (
        ('1', '入库'),
        ('2', '出库'),
        ('3','初始化')
    )
    operation_type = models.CharField(
        max_length = 8,
        choices=operation_type_choices,
    )    # 操作类型
    memo = models.TextField(null=True)    # 备注

    stock = models.ForeignKey('StockInfo', on_delete=models.CASCADE,db_constraint=False)
    operator = models.ForeignKey('UserInfo',on_delete=models.SET_NULL,blank=True,null=True,db_constraint=False)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = "operation_record"

